package com.example.java.base.rabbitmq.a2workqueue;


import com.example.java.base.rabbitmq.util.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Producer:生产消息 一对多的发消息,消费者可以平均分摊消息或者能者多劳
 *
 * @author zhangxiaoxiang
 * @date 2020/11/15
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取connection
        Connection connection = RabbitMqUtil.getConnection();
        //获取channel
        Channel channel = connection.createChannel();
        //	通过通道声明队列
        channel.queueDeclare("work", true, false, false, null);
        //	生产消息
        for (int i = 0; i < 200; i++) {
            channel.basicPublish("", "work", null, (i + "长草颜团子 workquene ").getBytes());
        }
        System.out.println("消息生产完毕");
        //	关闭资源
        RabbitMqUtil.closeConnectionAndChanel(channel, connection);
    }
}
